package com.da.javatest.thread.juc.guava;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import lombok.extern.slf4j.Slf4j;

/**
 * @author chenlida
 * @date 2020/7/18 23:40
 * 异步执行任务完毕之后回调
 */
@Slf4j
public class GuavaThread1 {
    public static void main(String[] args) {
        //创建一个线程池
        ExecutorService delegate = Executors.newFixedThreadPool(5);

        try {
            ListeningExecutorService executorService = MoreExecutors.listeningDecorator(delegate);
            //异步执行一个任务
            ListenableFuture<Integer> future = executorService.submit(() -> {
                log.info("{}", System.currentTimeMillis());
                //休眠2秒，默认耗时
                TimeUnit.SECONDS.sleep(2);
                log.info("{}", System.currentTimeMillis());
                return 10;
            });
            //当任务执行完毕之后回调对应的方法
            future.addListener(() -> log.info("任务执行完毕了，我被回调了"), MoreExecutors.directExecutor());
        } finally {
            delegate.shutdown();
        }
    }
}
